package com.zj.leetcode.hard;

/**
 * @program: algorithm
 * @description: 整数转换英文表示
 * 273 integer-to-english-words
 * //将非负整数 num 转换为其对应的英文表示。
 * //
 * //
 * //
 * // 示例 1：
 * //
 * //
 * //输入：num = 123
 * //输出："One Hundred Twenty Three"
 * //
 * //
 * // 示例 2：
 * //
 * //
 * //输入：num = 12345
 * //输出："Twelve Thousand Three Hundred Forty Five"
 * //
 * //
 * // 示例 3：
 * //
 * //
 * //输入：num = 1234567
 * //输出："One Million Two Hundred Thirty Four Thousand Five Hundred Sixty Seven"
 * //
 * //
 * // 示例 4：
 * //
 * //
 * //输入：num = 1234567891
 * //输出："One Billion Two Hundred Thirty Four Million Five Hundred Sixty Seven Thous
 * //and Eight Hundred Ninety One"
 * //
 * //
 * //
 * //
 * // 提示：
 * //
 * //
 * // 0 <= num <= 231 - 1
 * //
 * // Related Topics 递归 数学 字符串
 * // 👍 248 👎 0
 * @author: Zhang Bo
 * @create: 2022-01-14 17:05
 **/
public class IntegerToEnglishWords {
    public static void main(String[] args) {
        String str = "1,234,567,891"; // String str = "9,987,891";
//        Integer num = 1001;
//        int num = 1001;
//        int num = 1200;
//        int num = 12345;
        int num = 50868;
//        int num = 1000000;
//        int num = 1000010;  934 567 891
        int[] nums = new int[]{0, 100, 1001, 1200, 50868, 12345, 1000000, 1000010, 1234567891};
        Solution solution = new IntegerToEnglishWords().new Solution();

        System.out.println(solution.numberToWords02(num));

//        for (int i = 0; i < nums.length; i++) {
////            System.out.println(solution.numberToWords(nums[i]));
//            System.out.println(solution.numberToWords02(nums[i]));
//        }

    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        String[] singles = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"};
        String[] teens = {"Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"};
        String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};
        String[] thousands = {"", "Thousand", "Million", "Billion"};

        public String numberToWords(int num) {
            if (num == 0) {
                return "Zero";
            }


            String str = String.valueOf(num);
            StringBuffer ret = new StringBuffer();
            int sub = str.length() - 1 + 3;
            int commaNum = 0;

            comma(sub, str, ret, commaNum);

            return ret.toString().trim();
        }


        private void comma(int sub, String str, StringBuffer ret, int commaNum) {
            sub = sub - 3;
            ++commaNum;
            if (sub >= 3) {
                this.comma(sub, str, ret, commaNum);
            }
            --commaNum;
            this.hundredSub(str, sub, ret, commaNum);


        }


        private void hundredSub(String str, int sub, StringBuffer ret, int commaNum) {
            int a = 0;
            if (sub >= 1) {
                if (sub >= 2 && str.charAt(sub - 2) != 48) {
                    ret.append(singles[Character.getNumericValue(str.charAt(sub - 2))]);
                    ret.append(" Hundred ");
                    ++a;
                }
                int num = Integer.parseInt(str.substring(sub - 1, sub + 1));
                if (num < 20 && num > 10) {
                    ret.append(teens[Character.getNumericValue(str.charAt(sub))]).append(" ");
                    ++a;
                } else {
                    if (str.charAt(sub - 1) != 48) {
                        ret.append(tens[Character.getNumericValue(str.charAt(sub - 1))]).append(" ");
                        ++a;
                    }
                    if (str.charAt(sub) != 48) {
                        ret.append(singles[Character.getNumericValue(str.charAt(sub))]).append(" ");
                        ++a;
                    }

                }

            }
            if (sub == 0) {
                ret.append(singles[Character.getNumericValue(str.charAt(sub))]).append(" ");
                ++a;
            }

            if (a > 0) {
                ret.append(thousands[commaNum]).append(" ");
            }


        }


        public String numberToWords02(int num) {
            if (num == 0) {
                return "Zero";
            }
            StringBuffer ret = new StringBuffer();
            //1234567891  Billion
            int j = 3;
            int sub = 0;
            int newNum = num;
            for (int i = 1000000000; i >= 1; i /= 1000) {

                sub = newNum / i;
                if (sub != 0) {
                    if (sub >= 100) {
                        ret.append(singles[sub / 100]);
                        ret.append(" Hundred ");
                        sub = sub % 100;
                    }
                    if (sub >= 20) {
                        ret.append(tens[sub / 10]).append(" ");
                        if (sub % 10 > 0){
                            ret.append(singles[sub % 10]).append(" ");
                        }

                    } else if (sub >= 10) {
                        ret.append(teens[sub % 10]).append(" ");
                    }
                    if (sub < 10 && sub > 0) {
                        ret.append(singles[sub]).append(" ");
                    }
                    ret.append(thousands[j]).append(" ");
                }

                --j;
                newNum = newNum % i;

            }

            return ret.toString().trim();
        }

    }
//leetcode submit region end(Prohibit modification and deletion)

}
